<?php

namespace app\api\controller\driver;

use app\common\controller\Api;
use app\common\service\driver\OrderService;
use think\Validate;

/**
 * 司机端-订单中心接口
 * @ApiWeigh (86)
 */
class Order extends Api
{
    protected $beforeActionList = [
        'detail_before' => ['only' => 'detail,pay'],
        'trace_before' => ['only' => 'trace'],
        'start_before' => ['only' => 'start'],
        'end_before' => ['only' => 'end'],
    ];
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @ApiInternal
     */
    public function detail_before()
    {
        $params = $this->request->post();
        $rule = [
            'order_id|订单号' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }

    /**
     * @ApiInternal
     */
    public function trace_before()
    {
        $params = $this->request->post();
        $rule = [
            'oid|订单ID' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }

    /**
     * @ApiInternal
     */
    public function start_before()
    {
        $params = $this->request->post();
        $rule = [
            'order_id|订单号' => 'require|gt:0',
            'start_mileage|车辆公里数' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }

    /**
     * @ApiInternal
     */
    public function end_before()
    {
        $params = $this->request->post();
        $rule = [
            'order_id|订单号' => 'require|gt:0',
            'end_code|交车码' => 'require',
            'end_mileage|车辆公里数' => 'require|gt:0',
        ];
        $message = [];
        $validate = new Validate($rule, $message);
        if (!$validate->check($params)) {
            $this->error($validate->getError());
        }
    }


    /**
     * 订单列表
     *
     * @ApiMethod (POST)
     * @ApiParams (name="page", type="integer", required=true, description="页数")
     * @ApiParams (name="page_size", type="integer", required=true, description="每页数量")
     * @ApiParams (name="status", type="integer", required=false, description="订单状态：11=已接单|包含已接单，验车完成,15=已完成,2=已取消 不传值默认全部")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturnParams (name=data.total,type="integer",description=总数量)
     * @ApiReturnParams (name=data.data,type="array",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"订单列表","time":"1720497307","data":{"total":3,"data":[{"id":9,"channel_label":"渠道标签","user_id":"用户ID","driver_id":"司机ID","order_id":"订单号","out_order_id":"平台订单ID","avatar":"用户头像","user_name":"用户姓名","user_phone":"用户手机号","start_area_id":"起点区域ID","start_lat":"起点纬度","start_lng":"起点经度","start_poi_name":"起点Poi名称","start_address":"起点地址","end_area_id":"终点区域ID ","end_lat":"终点纬度","end_lng":"终点经度","end_poi_name":"终点Poi名称","end_address":"终点地址","appointment_time":"预约时间，时间戳 无预约时间表示即时订单","pay_type":"费用类型","rights_id":"权益ID","service_type":"服务类型:DRIVE=代驾,TRANSFER=取送车","createtime":"2024-07-09 11:13:15","updatetime":"2024-07-09 11:43:15","cancel_reason":"取消原因","status":"订单状态:1=已受理,2=已取消,11=已接单,13=验车完成,15=交车完成","desc":"订单备注","mileage":"里程（公里）","total_fee":"总金额","driver_amount":"佣金金额","accept_time":"接单时间","start_time":"验车时间","is_end":"是否已点击交车按钮:0=否,1=是","end_code":"交车码","end_time":"完成时间（交车时间 取消时间）","channel": {"name": "渠道名称","label": "渠道标签"}}]}})
     **/
    public function list()
    {
        $page = $this->request->post('page');
        $page_size = $this->request->post('page_size');
        $status = $this->request->post('status');
        $user = $this->auth->getUser();
        $list = OrderService::list_driver($this->request->controller() . '/' . __FUNCTION__, $user, $page, $page_size, $status);
        $this->success('订单列表', $list);
    }

    /**
     * 订单详情
     *
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="string", required=true, description="订单号")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code":1,"msg":"订单详情","time":"1720497307","data":{"id":9,"channel_label":"渠道标签","user_id":"用户ID","driver_id":"司机ID","order_id":"订单号","out_order_id":"平台订单ID","avatar":"用户头像","user_name":"用户姓名","user_phone":"用户手机号","start_area_id":"起点区域ID","start_lat":"起点纬度","start_lng":"起点经度","start_poi_name":"起点Poi名称","start_address":"起点地址","end_area_id":"终点区域ID ","end_lat":"终点纬度","end_lng":"终点经度","end_poi_name":"终点Poi名称","end_address":"终点地址","appointment_time":"预约时间，时间戳 无预约时间表示即时订单","pay_type":"费用类型","rights_id":"权益ID","service_type":"服务类型:DRIVE=代驾,TRANSFER=取送车","createtime":"2024-07-09 11:13:15","updatetime":"2024-07-09 11:43:15","cancel_reason":"取消原因","status":"订单状态:1=已受理,2=已取消,11=已接单,13=验车完成,15=交车完成","desc":"订单备注","mileage":"里程（公里）","total_fee":"总金额","driver_amount":"佣金金额","accept_time":"接单时间","start_time":"验车时间","is_end":"是否已点击交车按钮:0=否,1=是","end_code":"交车码","end_time":"完成时间（交车时间 取消时间）"}})
     **/
    public function detail()
    {
        $order_id = $this->request->post('order_id');
        $user = $this->auth->getUser();
        $info = OrderService::detail_driver($this->request->controller() . '/' . __FUNCTION__, $order_id, $user);
        $this->success('订单详情', $info);
    }

    /**
     * 行驶轨迹
     *
     * @ApiMethod (POST)
     * @ApiParams (name="oid", type="integer", required=true, description="订单ID")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "行驶轨迹","time": "1721463389","data": {"id": 7,"waypoints": "全部途经点坐标串 118.352522,35.101954;118.353156,35.10212;118.35064,35.10209;118.348531,35.102059;118.346128,35.102063","waypoints_16": "16个途经点坐标串 118.352522,35.101954;118.348531,35.102059","json": "高德路线规划"}})
     **/
    public function trace()
    {
        $oid = $this->request->post('oid');
        $info = OrderService::detail_driver($this->request->controller() . '/' . __FUNCTION__, $oid, []);
        $this->success('行驶轨迹', $info);
    }

    /**
     * 验车
     *
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="string", required=true, description="订单号")
     * @ApiParams (name="start_mileage", type="string", required=true, description="车辆公里数")
     * @ApiParams (name="start_images", type="string", required=false, description="验车图片（多张用英文逗号隔开）")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "验车成功","time": "1694660842","data": null})
     **/
    public function start()
    {
        $order_id = $this->request->post('order_id');
        $start_mileage = $this->request->post('start_mileage');
        $start_images = $this->request->post('start_images');
        $params = compact("start_mileage", "start_images");
        $user = $this->auth->getUser();
        OrderService::make_driver($order_id, $params, $user, __FUNCTION__);
        $this->success('验车成功');
    }

    /**
     * 支付
     *
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="string", required=true, description="订单号")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ([{"code":1,"msg":"支付信息","time":"1721461681","data":{"paytype":"bank","apptype":"","response":{"end_code":"1683"}}},{"code":1,"msg":"支付信息","time":"1721011845","data":{"paytype":"wechat","apptype":"scan","response":{"return_code":"SUCCESS","return_msg":"OK","result_code":"SUCCESS","mch_id":"1624453568","appid":"wx18c9ba38b467a5c2","nonce_str":"lrox0u6Dd98JbzsE","sign":"90642CE03BD1966411D8C54C791AE146","prepay_id":"wx151050456815858de26f97bc3d73f50000","trade_type":"NATIVE","code_url":"weixin://wxpay/bizpayurl?pr=7stwIeSz1"}}}])
     **/
    public function pay()
    {
        $order_id = $this->request->post('order_id');
        $user = $this->auth->getUser();
        $result = OrderService::pay_driver($order_id, $user);
        $this->success('支付信息', $result);
    }

    /**
     * 交车
     *
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="string", required=true, description="订单号")
     * @ApiParams (name="end_code", type="string", required=true, description="交车码")
     * @ApiParams (name="end_mileage", type="string", required=true, description="车辆公里数")
     * @ApiParams (name="end_car_image", type="string", required=false, description="交车合影图片")
     * @ApiParams (name="end_dashboard_image", type="string", required=false, description="仪表盘图片")
     * @ApiReturnParams (name=data,type="object",description=要返回的数据)
     * @ApiReturn ({"code": 1,"msg": "交车成功","time": "1694660842","data": null})
     **/
    public function end()
    {
        $order_id = $this->request->post('order_id');
        $end_code = $this->request->post('end_code');
        $end_mileage = $this->request->post('end_mileage');
        $end_car_image = $this->request->post('end_car_image');
        $end_dashboard_image = $this->request->post('end_dashboard_image');
        $params = compact("end_code", "end_mileage", "end_car_image", "end_dashboard_image");
        $user = $this->auth->getUser();
        OrderService::make_driver($order_id, $params, $user, __FUNCTION__);
        $this->success('交车成功');
    }

}
